package genia.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import genia.entity.User;

/**
 * @description: 实现UserDao接口
 * @author: WALKER
 * @date: 2022/4/9 22:15
 */

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public User userLogin(User user) {
        if(jdbcTemplate == null){
            System.out.println("jdbcTemplate为空指针");
        }
        String sql="select * from user where userName=?";
        BeanPropertyRowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
        User resUser;
        try{
            resUser = jdbcTemplate.queryForObject(sql,rowMapper,user.getUserName());
        }catch (DataAccessException e) {
            return null;
        }
        return resUser;
    }



}
